﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 工具
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public readonly string path = AppDomain.CurrentDomain.BaseDirectory;
        private void button1_Click(object sender, EventArgs e)
        {
            //初始化动态类的属性，并且必须要赋初值，全部用字符串表示，
            List<string[]> Propertys = new List<string[]>
                {
                    new string[]{ "string","StudentNo","编号"},//byte类型，初始值0或类似(byte)100
                    new string[]{ "int","Level","等级"},//字符型，用单引号加字符
                    new string[]{ "DateTime", "WorkDate", "日期" },//short类型，用0或类似(short)100
                    new string[] { "int", "ID" ,"ID"},
                    new string[] { "string","UserName","姓名"},
                    new string[]{"bool","Sex","性别" },
                    new string[]{"DateTime","JobTime","工作时间" },
                    new string[]{ "float","Scores","分数)"},
                    new string[]{ "double","PhonePower","能力"}
                };

            //创建动态类，构建一个程序集
            string className = "Person";
            string content = DynamicClassHelper.Newassembly(className, Propertys);
            string filepath = Path.Combine(path, "test.cs");
            //if (!File.Exists(filepath))
            //{
            //    File.Create(filepath);
            //}
            //using (FileStream stream = File.OpenWrite(filepath))
            //{
            //    byte[] bytes = Encoding.UTF8.GetBytes(content);
            //    stream.Write(bytes, 0, bytes.Length);
            //}
            File.WriteAllText(filepath, content);


            ////构建动态类的实例
            //dynamic objclass = assembly.CreateInstance(className);
            ////给动态类属性赋值，byte/short/unshort/long/float要强制转换
            //DynamicClassHelper.ReflectionSetValue(objclass, "studentNo", (byte)10);
            //DynamicClassHelper.ReflectionSetValue(objclass, "level", 'A');
            //DynamicClassHelper.ReflectionSetValue(objclass, "workdate", (short)300);
            //DynamicClassHelper.ReflectionSetValue(objclass, "id", 1);
            //DynamicClassHelper.ReflectionSetValue(objclass, "userName", "XiaoMing Li");
            //DynamicClassHelper.ReflectionSetValue(objclass, "sex", true);
            //DynamicClassHelper.ReflectionSetValue(objclass, "jobTime", new DateTime(2015, 8, 1));
            //DynamicClassHelper.ReflectionSetValue(objclass, "scores", new List<float> { (float)98.2, (float)95.0, (float)88.8 });
            //DynamicClassHelper.ReflectionSetValue(objclass, "phonePower", 0.89);

            ////获取动态类相应的属性值，因为返回的是object类，要强制转换后使用(List测试时，可以省略强制转换)
            //double phonePower = (double)DynamicClassHelper.ReflectionGetValue(objclass, "phonePower");
            //List<float> scores = (List<float>)DynamicClassHelper.ReflectionGetValue(objclass, "scores");

        }

        private void button2_Click(object sender, EventArgs e)
        {
            GetObjectInfo gb = new GetObjectInfo();
            gb.GetPros();
        }
    }
   
}
